package com.bazi;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 天干地支工具类
 */
public class HeavenlyStemEarthlyBranch {

    // 天干
    private static final String[] HEAVENLY_STEMS = {
        "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸"
    };

    // 地支
    private static final String[] EARTHLY_BRANCHES = {
        "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"
    };

    // 地支对应的生肖
    private static final String[] ZODIAC = {
        "鼠", "牛", "虎", "兔", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪"
    };

    // 五行
    private static final String[] FIVE_ELEMENTS = {
        "木", "木", "火", "火", "土", "土", "金", "金", "水", "水"
    };

    // 十神 (以日干为己土为例)
    // 比肩:戊, 劫财:己, 食神:庚, 伤官:辛, 偏财:壬, 正财:癸, 七杀:甲, 正官:乙, 偏印:丙, 正印:丁
    // 这里为了简化，我们只考虑日干为己土的情况，实际应用中需要动态计算
    private static final Map<String, String> TEN_GODS = new HashMap<>();
    static {
        TEN_GODS.put("甲", "七杀");
        TEN_GODS.put("乙", "正官");
        TEN_GODS.put("丙", "偏印");
        TEN_GODS.put("丁", "正印");
        TEN_GODS.put("戊", "比肩");
        TEN_GODS.put("己", "日主");
        TEN_GODS.put("庚", "食神");
        TEN_GODS.put("辛", "伤官");
        TEN_GODS.put("壬", "偏财");
        TEN_GODS.put("癸", "正财");
        TEN_GODS.put("子", "偏财");
        TEN_GODS.put("丑", "比肩");
        TEN_GODS.put("寅", "七杀");
        TEN_GODS.put("卯", "正官");
        TEN_GODS.put("辰", "比肩");
        TEN_GODS.put("巳", "正印");
        TEN_GODS.put("午", "正印");
        TEN_GODS.put("未", "比肩");
        TEN_GODS.put("申", "食神");
        TEN_GODS.put("酉", "食神");
        TEN_GODS.put("戌", "比肩");
        TEN_GODS.put("亥", "偏财");
    }

    /**
     * 根据年份获取天干地支
     * @param year 公历年份
     * @return 天干地支字符串
     */
    public static String getYearHeavenlyStemEarthlyBranch(int year) {
        // 1984年是甲子年
        int offset = year - 1984;
        int stemIndex = (offset + 10) % 10;
        if (stemIndex < 0) stemIndex += 10;
        int branchIndex = (offset + 12) % 12;
        if (branchIndex < 0) branchIndex += 12;
        return HEAVENLY_STEMS[stemIndex] + EARTHLY_BRANCHES[branchIndex];
    }

    /**
     * 根据日期获取天干地支 (这里简化处理，实际需要复杂的算法)
     * @param date 日期
     * @return 天干地支字符串
     */
    public static String getDateHeavenlyStemEarthlyBranch(LocalDate date) {
        // 这里只是一个示例，实际需要根据具体的算法来计算
        // 比如通过一个基准日期来推算
        // 为了演示，我们假设1987年6月5日是己酉日
        if (date.equals(LocalDate.of(1987, 6, 5))) {
            return "己酉";
        }
        // 其他日期需要具体算法
        return "未知";
    }

    /**
     * 根据时辰获取天干地支 (这里简化处理)
     * @param hour 时辰 (0-23)
     * @return 天干地支字符串
     */
    public static String getHourHeavenlyStemEarthlyBranch(int hour) {
        // 时辰地支是固定的
        int branchIndex = (hour + 1) / 2 % 12;
        if (branchIndex == 0) branchIndex = 12; // 丑时是1-3点，对应索引1
        branchIndex--; // 转换为数组索引
        // 时辰天干需要根据日干来推算，这里简化处理
        // 假设日干是己土，那么时辰天干是乙
        return "乙" + EARTHLY_BRANCHES[branchIndex];
    }

    /**
     * 获取生肖
     * @param year 公历年份
     * @return 生肖字符串
     */
    public static String getZodiac(int year) {
        int offset = year - 1984;
        int index = (offset + 12) % 12;
        if (index < 0) index += 12;
        return ZODIAC[index];
    }

    /**
     * 获取天干五行
     * @param stem 天干
     * @return 五行
     */
    public static String getStemElement(String stem) {
        for (int i = 0; i < HEAVENLY_STEMS.length; i++) {
            if (HEAVENLY_STEMS[i].equals(stem)) {
                return FIVE_ELEMENTS[i];
            }
        }
        return "未知";
    }

    /**
     * 获取地支五行
     * @param branch 地支
     * @return 五行
     */
    public static String getBranchElement(String branch) {
        for (int i = 0; i < EARTHLY_BRANCHES.length; i++) {
            if (EARTHLY_BRANCHES[i].equals(branch)) {
                // 地支五行需要特殊处理
                switch (branch) {
                    case "寅": case "卯": return "木";
                    case "巳": case "午": return "火";
                    case "申": case "酉": return "金";
                    case "亥": case "子": return "水";
                    case "辰": case "戌": case "丑": case "未": return "土";
                    default: return "未知";
                }
            }
        }
        return "未知";
    }

    /**
     * 获取十神 (简化版)
     * @param pillar 柱子的天干或地支
     * @return 十神
     */
    public static String getTenGod(String pillar) {
        return TEN_GODS.getOrDefault(pillar, "未知");
    }
}